/***************************************************************************************
Firm-embedded productivity and cross-country income differences
Alviarez, Cravino and Ramondo
Journal of Political Economy (2022)

Program: figure_A6_management_practices.do
Date: October 2022

Description: Reproduces Figure A.6: Firm-embedded productivity and management practices.

*****************************************************************************************/

*-------------------------------------------------------------------------------
global typeden=1
include "set_directories.do"
set memory 64g
global lf "LF"

*Log
cap log close
log using "${clogs}/figure_A6_management_practices.log",replace
*-------------------------------------------------------------------------------


*Get the phi's from each country for the set of countries for which 
*---------------------------------------------------------
clear all
use year isocode D_A using "${data}/estimates_agg_naics_sales_s1_base_woparent.dta", clear
keep if year==2016
gen D_A_phi=(-1)*D_A*(0.2)
keep if isocode=="FR" | isocode=="DE" | isocode=="GB" | isocode=="GR" | isocode=="IT" | isocode=="JP" | isocode=="MX" | isocode=="PL" | isocode=="PT" | isocode=="ES" | isocode=="SE"
tempfile phi
save `phi', replace 


*Bring the management practice dataset
*--------------------------------------------------------- 
clear all
import delimited "${data}\wmsdata_2004_2015.csv", encoding(UTF-8) 

keep if country=="France" | country=="Germany" | country=="Great Britain" | country=="Greece" | country=="Italy" | country=="Japan" | country=="Mexico" | country=="Poland" | country=="Portugal" | country=="Spain" | country=="Sweden"
gen isocode="NA"
replace isocode="FR" if country=="France"
replace isocode="DE" if country=="Germany"
replace isocode="GB" if country=="Great Britain"
replace isocode="GR" if country=="Greece"
replace isocode="IT" if country=="Italy"
replace isocode="JP" if country=="Japan"
replace isocode="MX" if country=="Mexico"
replace isocode="PL" if country=="Poland"
replace isocode="PT" if country=="Portugal"
replace isocode="ES" if country=="Spain"
replace isocode="SE" if country=="Sweden"

keep country management employment isocode
tab employment
*Drop small firms 
drop if employment=="A) 50 to 100" | employment=="B) 101 to 250"

collapse (mean) management, by(isocode)

gen xx=management if isocode=="FR" 
egen xx_max=max(xx)
gen management_relative=ln(management)-ln(xx_max)
drop xx* management


*Bring phi information
*--------------------------------------------------------- 
merge 1:1 isocode using `phi', keepusing(D_A_phi)
drop _merge



*Prepate graph 
*------------------------------------
gen ss=1
local abslist "ss"
local LHS D_A_phi
local RHS1 management_relative
local ytitle "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n}"
local xtitle "Average Management Practices"


reghdfe `LHS' `RHS1', abs(`abslist') 
mat b = e(b)
mat V = e(V)
global c1: display %-03.2fc round(b[1,1],0.001)
global se1: display %-03.2fc round(sqrt(V[1,1]),0.001)
global rsq1: display %-03.2fc round(`e(r2)',0.0001)

display $c1
display $se1
display $rsq1


global opt1 msymbol(circle_hollow) mlabel(isocode) mlabcolor(red)  msize(smaller) mlabsize(small)
global opt2 msymbol(square_hollow) mlabel(isocode) mlabcolor(blue) msize(smaller) mlabsize(small)
two (scatter `LHS' `RHS1', sort $opt1  mcolor(red)) (lfit `LHS' `RHS1', lcolor(red)) (line `RHS1' `RHS1', lcolor(none)) , ylabel(-0.4(0.2)0.2, labsize(medlarge)) xlabel(-0.08(0.04)0.04,  labsize(medlarge)) ///
	title("", size(small)) ytitle("`ytitle'", size(medlarge)) xtitle("`xtitle'", size(medlarge)) graphregion(fcolor(white) lcolor(white)) ///
	legend(order(2 "R2: $rsq1" ) rows(2) size(medlarge) bplace(se) ring(0) region(lwidth(none))) 

	
graph export "${rappendix}/fig_A6_management_practices.pdf", replace


log close 
	
	

